/*$(function() {
	var sortTypeId = getvl("sortTypeId");
	if(id != '') {
		searchBySortType(sortTypeId);
	}
});*/

//商圈
TradeArea();
//分类
sortType();
//属性
propertyType();

var currentPage = 1;
var tradeAreaId = '';
var sortTypeId = '';
var propertyTypeId = '';
var tradeNumber = '',
	goodsType = '',
	isReturnedOfSevenDays = '';
var sort = ''; //排序
//价格区间
var betweenMoney = '';

//列表
loadMyEssay(tradeAreaId, sortTypeId, propertyTypeId, tradeNumber, isReturnedOfSevenDays, sort, betweenMoney);

function loadMyEssay(tradeAreaId, sortTypeId, propertyTypeId, tradeNumber, isReturnedOfSevenDays, sort, betweenMoney) {
	$(document).ready(function() {
		init(currentPage);
	});

	function init(pageNo) {
		$("#goodsList").html("");
		$.ajax({
			url: urlcore + "/api/poop/selectPoopList?tradeAreaId=" + tradeAreaId + "&sortTypeId=" + sortTypeId + "&propertyTypeId=" + propertyTypeId + "&tradeNumber=" + tradeNumber + "&goodsType=" + goodsType + "&isReturnedOfSevenDays=" + isReturnedOfSevenDays + "&sort=" + sort + "&betweenMoney=" + betweenMoney + "&size=" + 32 + "&current=" + pageNo,
			type: "get",
			dataType: 'json',
			async: false,
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					$.each(data.data.list, function(i, n) {
						var coverUrl=n.goods.coverUrl==null?'http://app.jiagangwangluo.com/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':n.goods.coverUrl;
						coverUrl=isHasImg(coverUrl);//图片处理
						var goodsTitle=n.goods.goodsTitle==null?'暂无':n.goods.goodsTitle;
						var tradePrice=n.goods.tradePrice==null?'0':n.goods.tradePrice;
						var packPrice=n.goods.packPrice==null?'0':n.goods.packPrice;
						var collectNum=n.goods.collectNum==null?'0':n.goods.collectNum;
						
						var thislist =
							'<dl>' +
							'<a target="_blank" href="2.2.0goods_detail.html?id=' + n.goods.id + '&breadcrumbType=2">' +
							'<dt>' +
							'<img class="searchWeiHuoImg" src="' + coverUrl + '" />' +
							'</dt>' +
							'<dd>' +
							'<h5>' + goodsTitle + '</h5>' +
							'<h6>&yen;' + tradePrice + '-'+packPrice+'<span>收藏 ' + collectNum + '</span>' + '</h6>' +
							'</dd>' +
							'</a>' +
							'</dl>';
						$('#goodsList').append(thislist);
					});

                     //固定宽高
					$(function(){
						var imgList1 = document.getElementsByClassName("searchWeiHuoImg");  
					    var w=290;
					    var h=290;
					    for(var i=0;i<imgList1.length;i++){
					        if(imgList1[i].width>w || imgList1[i].height>h){  
					            imgList1[i].width = w;  
					            imgList1[i].heigth = h;  
					        }  
					    }  
					})

					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					alert(thisUrl);
					if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
						top.window.location.href = "1.0.0denglu.html";
					}

				} else {
					if(data.msg != '空数据') {
						layer.alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				layer.alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}


//按区域查询
function searchByTradeArea(tradeAreaId) {
	loadMyEssay(tradeAreaId, '', '', '', '', '', '');
}

//按分类查询
function searchBySortType(sortTypeId) {
	$.ajax({
		url: urlcore + "/api/sortType/selectOne?id=" + sortTypeId,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
			   $("#breadcrumbName").text(data.data.sortName);
			}
		},
		error: function() {
			layer.alert("error");
		}
	});

	loadMyEssay('', sortTypeId, '', '','', '', '');
}

//按属性查询
function searchByPropertyType(propertyTypeId) {
	loadMyEssay('', '', propertyTypeId, '','', '', '');
}

//搜索 一件起批    支持包换
function selectGoods(tradeNumber,isReturnedOfSevenDays) {

	loadMyEssay('', '', '', tradeNumber, isReturnedOfSevenDays, '', '');
}
//按时间,单价,收藏量,销售量排序
function selectGoodsOrderBySort(sort) {
	loadMyEssay('', '', '', '', '', sort, '');
}
//按价格区间以及起批量查询 
function selectGoodsByMoneyOrTradeNumber() {
	var minPprice = $("#min_price").val().trim();
	var maxPrice = $("#max_price").val().trim();
	var betweenMoney = '';
	if(minPprice != '' && minPprice != null && maxPrice != '' && maxPrice != null) {
		betweenMoney = minPprice + "-" + maxPrice;
	}
	var retailPum = $("#retail_num").val().trim();

	loadMyEssay('', '', '', retailPum,'', '', betweenMoney);
}
 